//
//  VideoCollectionViewController.m
//  WWXELuShang
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "VideoCollectionViewController.h"
#import "VideoCollectionViewCell.h"
#import "PlayVideoViewController.h"
#import "VideoModel.h"
#import "MJRefresh.h"

@interface VideoCollectionViewController ()<UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)NSMutableArray *videoArray;

@end

@implementation VideoCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

static NSInteger i = 1;

- (NSMutableArray *)videoArray {
    if (!_videoArray) {
        _videoArray = [[NSMutableArray alloc] init];
        
    }
    return _videoArray;
}

- (void)jsonVideoUrl {
    NSString *page = [NSString stringWithFormat:@"%ld", i];
    [LORequestManger GET:[NSString stringWithFormat:kVideoUrl, page] success:^(id response) {
        NSDictionary *rootDic = (NSDictionary *)response;
        for (NSDictionary *dic in rootDic[@"data"][@"list"]) {
            VideoModel *videoModel = [[VideoModel alloc] init];
            [videoModel setValuesForKeysWithDictionary:dic];
            [self.videoArray addObject:videoModel];
        }
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    i ++;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    i = 1;
    
    [self jsonVideoUrl];
    
    self.collectionView.backgroundColor = kBackGroundColor;
    __weak VideoCollectionViewController *videoCVC = self;
    [self.collectionView addFooterWithCallback:^{
        [videoCVC jsonVideoUrl];
        [videoCVC.collectionView footerEndRefreshing];
    }];
    [self.collectionView addHeaderWithCallback:^{
        [videoCVC.collectionView headerEndRefreshing];
    }];
    self.collectionView.backgroundColor = kBackGroundColor;
    
    
    [self.collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return self.videoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    VideoModel *video = self.videoArray[indexPath.row];
    VideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.videoModel = video;
    // Configure the cell
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(kWidth / 20 * 9, kWidth / 20 * 9);
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    
    return UIEdgeInsetsMake(kWidth / 40, kWidth / 40, kWidth / 40, kWidth / 40);
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoModel *video = self.videoArray[indexPath.row];
    PlayVideoViewController *playVideoVC = [[PlayVideoViewController alloc] init];
    playVideoVC.urlString = video.Mp4Link;
    playVideoVC.time = video.Duration;
    [self presentViewController:playVideoVC animated:NO completion:nil];
    
    
    
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
